package com.example.administrator.myapplication.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.myapplication.Adapter.InShipinAdapter;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.ShipinFragment.JieshuoFragment;
import com.example.administrator.myapplication.ShipinFragment.SaishiFragment;
import com.example.administrator.myapplication.ShipinFragment.YuleFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/5.
 */
public class shipinfragment extends Fragment {
    private Context context;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> fragments;
    private String[] titles={"娱乐","解说","赛事"};
    private InShipinAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shipinfragment, null);
        tabLayout=(TabLayout) view.findViewById(R.id.tablayout);
        viewPager=(ViewPager) view.findViewById(R.id.vp);
        //添加title
        for (int i=0;i<titles.length;i++){
            //根据需要设置显示的标题添加到tab中
            tabLayout.addTab(tabLayout.newTab().setText(titles[i]));
        }
        //填充viewpager的数据
        initViewPager();
        //绑定viewpager的监听事件 参数传递的TabLayout提供的监听事件  viewpager中的page页与tab页对应
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        return view;
    }
    private void initViewPager() {
        fragments=new ArrayList<>();
         fragments.add(new YuleFragment());
         fragments.add(new JieshuoFragment());
         fragments.add(new SaishiFragment());
          adapter=new InShipinAdapter(getFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
    }
}

